package com.sxpi.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.sxpi.common.result.R;
import com.sxpi.order.mode.user.IPayService;
import com.sxpi.order.mode.user.UserBeanFactory;
import com.sxpi.order.mode.user.UserBeanFactorySpringSingle;
import com.sxpi.order.service.order.IOrderService;
import com.sxpi.order.vo.BuyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/26 22:28
 */
@RestController
@Slf4j
public class OrderBuyController extends BaseController{

    @Autowired
    private IOrderService orderService;

    /**
     * 购买课程下单
     * @param buyVo
     * @return
     */
    @PostMapping("/order/buy")
    @SaCheckLogin
    public R buyCourse(@RequestBody BuyVo buyVo){
        Map<String, Object> dataMap = StpUtil.getSession().getDataMap();
        // 拉起微信支付

        // 计算金额，不同的用户身份折扣和学习币是不一样的(原始的写法是if else if else一直判断vip等级，然后给不同的折扣，但是如果以后有别的身份进来呢？)
        // 策略者设计模式 工厂设计模式
/*
        // 方式一
        Integer vip = 1;
        IPayService iPayService = UserBeanFactory.getInstance(vip);
        Double money = 1000d;
        money = iPayService.getMoney(money);
*/
/*
        // 方式二
        Integer vip = 2;
        IPayService instanceByMap = UserBeanFactory.getInstanceByMap(vip);
        Double money = 1000d;
        money = instanceByMap.getMoney(money);
*/
        Integer vip = 2;
        IPayService instanceByMap = UserBeanFactorySpringSingle.getInstanceSingle(vip);
        Double money = 1000d;
        money = instanceByMap.getMoney(money);



        // id不建议在前台传递，这样会安全一点点
        Object loginId = StpUtil.getLoginId();
        buyVo.setUserId(Long.parseLong(String.valueOf(loginId)));
        String username = MapUtil.getStr(dataMap, "username");
        String avatar = MapUtil.getStr(dataMap, "avatar");
        buyVo.setUsername(username);
        buyVo.setAvatar(avatar);
        return orderService.buyCourse(buyVo);
    }
}
